//INCLUDE :

#include <cstdlib>
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <pwd.h>
#include "../socks/sock.h"
#include "../socks/sockdist.h"

using namespace std;


int main(int argc, char** argv)
{
	Sock BRpublic(SOCK_STREAM, (short)54001, 0);
	int descBRpublic;
	
	if(BRpublic.good())
	{
		descBRpublic = BRpublic.getsDesc();
	}
	
	while(true)
	{
		int res = listen(descBRpublic, 5);
		
		struct sockaddr_in BRcv;
		socklen_t lgBRcv = sizeof(struct sockaddr_in);
		int descBRcv = accept(descBRpublic, (struct sockaddr*)&BRcv, &lgBRcv);
		
		if(descBRcv == -1)
		{
			cout << "erreur" << endl;
			exit(0);
		}
		
		switch( uid_t fils = fork() )
		{
			case -1 :
			{	cout << "erreur" << endl;
			}	break;
				
			case 0 : // fils
			{	cout << "entre fils" << endl;
				sleep(5);
				cout << "sortie fils" << endl;
				close(descBRcv);
				exit(0);
			}	
			default :
			{	cout << "le pere continu" << endl;
			}	break;
		}
	}
}
